home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!usc!usenet
- From: Zahir Yilmaz Alpaslan <alpaslan>
- Newsgroups: comp.lang.c
- Subject: beginner needs help about langtons's ant
- Date: 20 Apr 1996 23:27:14 GMT
- Organization: University of Southern California
- Sender: alpaslan@sal-sun36.usc.edu
- Message-ID: <4lbrsi$4o@usc.edu>
- NNTP-Posting-Host: sal-sun36.usc.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.12 (X11; I; SunOS 4.1.4-PL sun4m)
- X-URL: news:comp.lang.c
-
- I am trying to implement langton's ant in c.
- This ant moves rigth if it's currently in a white grid and moves left if it's
- currently in a black grid , and changes grid color before moving to the other
- one. It should begin with all black grids or all empty(white) grids, to it's
- movement. And it should move at least 10800 times to make a highway patern.
-
-
- When I tried to run this program in unix environment it gave a segmentation
- fault, where did I go wrong?
-
-
-
- #include<stdio.h>
- char a[100][100];
-
- int c;
- void grid(char);
- void regular(int, int);
- void castle(void);
- int w(int);
- int e(int);
- int s(int);
- int n(int);
- void antd(char);
-
- main()
- {
- int e,i, j;
-
-
- i = 50;
- j = 50;
- printf("\n\n%s\n\n\n\n",
- "Hi, do you want to begin with empty grid or full one\n"
- "for empty one enter nothing for the other enter * ");
- scanf("%c", &e);
- grid(e);
- while (c <= 10800)
- regular(i,j);
- }
-
-
- void grid(char e)
- {
-
- int i,j;
- for(i=0;i<=100;i++);
- for(j=0;j<=100;j++);
- a[i][j]=e;}
-
-
- void regular(int i, int j)
- {
- char a[100][100], b;
-
- if (a[i][j] == NULL)
- {a[i][j]='*';
- e(i);
- if (a[i][j] == NULL)
- {a[i][j]='*';
- s(j);
- if (a[i][j] == NULL)
- {a[i][j]='*'; w(i);}
- else {a[i][j]=NULL;
- e(i);}
- } else {a[i][j]=NULL;
- n(j);}
- if (a[i][j] == NULL){a[i][j]='*';
- e(i);}
- else{ a[i][j]=NULL;
- w(i);}
- } else
- {
- w(i);a[i][j]=NULL;
- if (a[i][j] == NULL){a[i][j]='*';
- n(j);}
- else {a[i][j]=NULL;
- s(j);}
- } for (j = 0; j < 100; ++j)
- fprintf( "%c", a[i][j]);
- fprintf( "\n");
-
- }
-
-
-
-
- int n(int j)
- {
- c = c + 1;
- return (j++);
- }
- int e(int i)
- {
- c = c + 1;
- return (i++);
- }
- int w(int i)
- {
- c = c + 1;
- return (i--);
- }
- int s(int j)
- {
- c = c + 1;
- return (j--);
- }
-
- --
-
- ##########################################
- # #
- # Zahir Yilmaz Alpaslan #
- # Minister of Treasures of Cthulhu #
- # http://www-scf.usc.edu/~alpaslan #
- # University of Southern California #
- # Department of Electrical Engineering #
- # #
- ##########################################
-
-